home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 1 / CU Amiga Magazine CD-ROM Special Edition (1995)(EMAP Images)(GB)[Issue 1995-11].iso / Aminet / comm / tcp / AmiTCPsdk_40.lha / AmiTCP-4.0 / src / netlib / _dup.c < prev    next >
C/C++ Source or Header  |  1994-09-29  |  3KB  |  115 lines

  1. RCS_ID_C="$Id: _dup.c,v 4.1 1994/09/29 23:09:02 jraja Exp $";
  2. /*
  3.  *      _dup.c - duplicate a file descriptor (SAS/C)
  4.  *
  5.  *      Copyright © 1994 AmiTCP/IP Group, 
  6.  *                       Network Solutions Development Inc.
  7.  *                       All rights reserved.
  8.  */
  9.  
  10. #include <ios1.h>
  11. #include <fcntl.h>
  12. #include <stdlib.h>
  13. #include <dos.h>
  14. #define USE_BUILTIN_MATH
  15. #include <string.h>
  16. #include <errno.h>
  17. #include <dos/dos.h>
  18. #include <proto/dos.h>
  19.  
  20. #include <bsdsocket.h>
  21.  
  22. /****** net.lib/dup ***********************************************************
  23.  
  24.     NAME
  25.         dup, dup2 - duplicate an existing file descriptor
  26.  
  27.     SYNOPSIS
  28.         #include <unistd.h>
  29.  
  30.         int dup(int oldd)
  31.  
  32.         int dup2(int oldd, int newd)
  33.  
  34.     FUNCTION
  35.         Dup() duplicates an existing object descriptor and returns its value
  36.         to the calling program (newd = dup(oldd)). The argument oldd is a
  37.         small nonnegative integer index in the program's descriptor table.
  38.         The value must be less than the size of the table, which is returned
  39.         by getdtablesize().  The new descriptor returned by the call is the
  40.         lowest numbered descriptor currently not in use by the program.
  41.  
  42.         The object referenced by the descriptor does not distinguish between
  43.         oldd and newd in any way.  Thus if newd and oldd are duplicate
  44.         references to an open file, read() and write() calls all move a single
  45.         pointer into the file, and append mode, non-blocking I/O and
  46.         asynchronous I/O options are shared between the references.  If a
  47.         separate pointer into the file is desired, a different object
  48.         reference to the file must be obtained by issuing an additional open()
  49.         call.  The close-on-exec flag on the new file descriptor is unset.
  50.  
  51.         In dup2(), the value of the new descriptor newd is specified.  If this
  52.         descriptor is already in use, the descriptor is first deallocated as
  53.         if a close() call had been done first.
  54.  
  55.     RETURN VALUES
  56.         The value -1 is returned if an error occurs in either call.  The
  57.         external variable errno indicates the cause of the error.
  58.  
  59.     BUGS
  60.         The current UFB implementation for SAS C allows only sockets to be
  61.         duplicated.
  62.  
  63.     ERRORS
  64.         Dup() and dup2() fail if:
  65.  
  66.         [EBADF]       Oldd or newd is not a valid active descriptor
  67.  
  68.         [EMFILE]      Too many descriptors are active.
  69.  
  70.     SEE ALSO
  71.         accept(),  open(),  close(),  socket(),  getdtablesize()
  72.  
  73.     STANDARDS
  74.         Dup() and dup2() are expected to conform to IEEE Std 1003.1-1988
  75.         (``POSIX'').
  76.  
  77.     COPYRIGHT
  78.         This manual page is copyright © 1980, 1991 Regents of the
  79.         University of California.  All rights reserved.
  80.  
  81. *******************************************************************************
  82. */
  83.  
  84.  
  85. int
  86. dup(int old_fd)
  87. {
  88.   struct UFB *ufb;
  89.   int ufbflg;
  90.   /*
  91.    * Check for the break signals
  92.    */
  93.   __chkabort();
  94.  
  95.   /*
  96.    * Find the ufb * for the given FD
  97.    */
  98.   if ((ufb = __chkufb(old_fd)) == NULL) {
  99.     errno = EBADF;
  100.     return -1;
  101.   }
  102.   
  103.   ufbflg = ufb->ufbflg;
  104.  
  105.   /* 
  106.    * The brain dead UFB system won't allow duplicating ordinary files
  107.    */
  108.   if ((ufbflg & UFB_SOCK) == UFB_SOCK) {
  109.     return Dup2Socket(old_fd, -1);
  110.   } else {
  111.     errno = EBADF;
  112.     return -1;
  113.   }
  114. }
  115.